package com.example.demo.controller;

import com.alibaba.fastjson.JSONArray;
import com.example.demo.dao.CommodityListDao;
import com.example.demo.dao.RepertorygoodsDao;
import com.example.demo.pojo.CommodityList;
import com.example.demo.pojo.Repertorygoods;
import com.example.demo.service.RepertorygoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@Api(value = "搜索框",tags = "搜索框")
public class ShangpingController {

    @Autowired
    private CommodityListDao commodityListDao;
    @Autowired
    private RepertorygoodsDao repertorygoodsDao;

    @RequestMapping(value = "/Searchbox",method = RequestMethod.POST)
    @ApiOperation(value = "搜索框",notes = "搜索框")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "搜索框文本",name = "Searchboxtext")
    })
    public String Searchbox(HttpSession httpSession, String Searchboxtext){
//        Cookie[] cookies=httpServletRequest.getCookies();

        int repertoryId=Integer.valueOf((String)httpSession.getAttribute("warehouse"));
//        for (int i = 0; i <cookies.length ; i++) {
//            Cookie cookie=cookies[i];
//            String cookieName=cookie.getName();
//            System.out.println(cookies[i].getValue());
//            System.out.println(cookieName);
//            if (cookieName.equals("warehouse")){
//                System.out.println("ni");
//                repertoryId=Integer.getInteger(cookie.getValue());
//                break;
//            }
//        }
        String a="";
        List<Repertorygoods> repertorygoods=repertorygoodsDao.selectGoods(repertoryId);
        System.out.println("我是仓库id========="+repertoryId);
        for (int i = 0; i <repertorygoods.size() ; i++) {
            int id=repertorygoods.get(i).getGoodsid();
            if (repertorygoods.size()-1==i){
                a=a+id;
            }else {
                a=a+id+",";
            }
        }
        List<CommodityList> commodityLists=commodityListDao.selectByreper(a,Searchboxtext);
        return JSONArray.toJSONString(commodityLists);
    }
}
